package com.example.testbroadcast;

import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testbroadcast.db.BlackDAO;
import com.example.testbroadcast.db.BlackDBHelper;
import com.example.testbroadcast.reciever.OutgoingCallReceiver;

import java.util.ArrayList;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {


    private EditText etNumber;
    private ArrayList<String> blackNumbers;
    private ListView lvNumbwer;
    private SQLiteDatabase db;
    private BaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[]perms={Manifest.permission.PROCESS_OUTGOING_CALLS};
        if(!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this,"111",1,perms);
        }
        OutgoingCallReceiver reciever=new OutgoingCallReceiver();
        IntentFilter intentfilter=new IntentFilter();
        intentfilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
        registerReceiver(reciever,intentfilter);
        final Button btnAdd = (Button) findViewById(R.id.btn_add);
        etNumber = (EditText) findViewById(R.id.et_number);
        lvNumbwer = (ListView) findViewById(R.id.lv_number);
        showBlackList();


        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加到数据库
                BlackDAO dao=new BlackDAO(getApplicationContext());
                String phone=etNumber.getText().toString();
                dao.insert(phone);
                blackNumbers.add(phone);

                etNumber.setText("");
                adapter.notifyDataSetChanged();//刷新
            }
        });

    }

    private void showBlackList() {

        blackNumbers = new ArrayList<>();
        BlackDBHelper helper=new BlackDBHelper(getApplicationContext());
        db=helper.getReadableDatabase();


        Cursor cursor = db.query("info", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(0);
            blackNumbers.add(number);
        }


        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return blackNumbers.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                LinearLayout linearLayout=new LinearLayout(getApplicationContext());
                TextView tv=new TextView(getApplicationContext());
                tv.setText(blackNumbers.get(position));
                Button btnDelete=new Button(getApplicationContext());
                btnDelete.setText("delete");
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.addView(tv);
                linearLayout.addView(btnDelete);
                btnDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int r=db.delete("info","phone=?",new String[]{blackNumbers.get(position)});
                        Log.i("Main", "onClick:delete "+r);
                        blackNumbers.remove(position);
                        notifyDataSetChanged();//刷新
                    }
                });

                return linearLayout;
            }
        };
        lvNumbwer.setAdapter(adapter);
    }


}
